﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Nexus.IPConfiguration.Configurator.NetworkAdapterConfigs;
using Microsoft.Win32;
using System.Reflection;
using Nexus.IPConfiguration.Configurator.Properties;
using System.Configuration;

namespace Nexus.IPConfiguration.Configurator
{
    public partial class SettingsForm : Form
    {
        private bool isShown;
        public SettingsForm()
        {
            InitializeComponent();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            BindData();
            chkAutorun.Checked = GetAutorun();
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\IpConfigurator");
            string locale = key.GetValue("Locale") as string;
            if (locale.ToLower() == "en-gb")
            {
                radioBtnEn.Checked = true;
            }
            else
            {
                radioBtnPl.Checked = true;
            }
            key.Dispose();
        }

        private void BindData()
        {
            bindingSourceNetworkAdapterConfiguration.DataSource = NetworkAdapterConfigsManager.Configs;
            listNetworkAdaptersConfigs.DataSource = bindingSourceNetworkAdapterConfiguration;
            bindingSourceNetworkAdapterConfiguration.ResetBindings(false);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            NetworkAdapterConfiguration newConfig = bindingSourceNetworkAdapterConfiguration.AddNew() as NetworkAdapterConfiguration;
            newConfig.Name = "Nowa";
            BindData();
            networkAdapterConfigUserControl1.NetworkAdapterConfig = newConfig;
        }

        private void SettingsForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            NetworkAdapterConfigsManager.SaveToFile();
        }

        private void bindingSourceNetworkAdapterConfiguration_CurrentChanged(object sender, EventArgs e)
        {
            BindingSource source = sender as BindingSource;
            NetworkAdapterConfiguration current = source.Current as NetworkAdapterConfiguration;
            networkAdapterConfigUserControl1.NetworkAdapterConfig = current;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            bindingSourceNetworkAdapterConfiguration.RemoveCurrent();
            BindData();
        }

        private void chkAutorun_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            SetAutorun(chk.Checked);
        }

        private void SetAutorun(bool useAutorun)
        {
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true))
            {
                if (useAutorun)
                {
                    if (key.GetValue("IpConfigurator") == null)
                    {
                        key.CreateSubKey("IpConfigurator");
                    }
                    key.SetValue("IpConfigurator", Assembly.GetEntryAssembly().Location);
                }
                else
                {
                    if (key.GetValue("IpConfigurator") != null)
                    {
                        key.DeleteValue("IpConfigurator");
                    }
                }
            }
        }

        private bool GetAutorun()
        {
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true))
            {
                if (key.GetValue("IpConfigurator") == null)
                {
                    return false;
                }
                else
                {
                    if ((key.GetValue("IpConfigurator") as string) == Assembly.GetEntryAssembly().Location)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        private void radioBtnEn_CheckedChanged(object sender, EventArgs e)
        {
            if (isShown == false)
            {
                return;
            }
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\IpConfigurator", true);
            if (radioBtnEn.Checked)
            {
                key.SetValue("Locale", "en-gb");
                MessageBox.Show("Language will be changed at the next program start");
            }
            else if (radioBtnPl.Checked)
            {
                key.SetValue("Locale", "pl-pl");
                MessageBox.Show("Język zostanie zmieniony podczas następnego uruchomienia programu");
            }
            key.Dispose();
        }

        private void SettingsForm_Shown(object sender, EventArgs e)
        {
            isShown = true;
        }
    }
}
